package rj.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="CountServlet",urlPatterns = "/CountServlet")
public class CountServlet extends HttpServlet {
    public void init() throws ServletException{
     int count=0;//给次数设置一个初始值0
     //将0存入到ServletContext中
    ServletContext context = getServletContext();
    context.setAttribute("count",count);
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //将原来的次数count从ServletContext中取出来
        int count=(int) getServletContext().getAttribute("count");
        count+=1;
        //存入到ServletContext中
        getServletContext().setAttribute("count",count);
        //向页面输出欢迎光临！
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("<h1>欢迎光临本网站！这是您第"+count+"次访问</h1>");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
    }

}